/**
 * 
 */
package gopi.labs.Connector;

import gopi.labs.exceptions.InValidIdentifier;
import gopi.labs.exceptions.InvalidCommunicationType;
import gopi.labs.exceptions.InvalidConnectionMode;
import gopi.labs.exceptions.InvalidDeviceClass;

/**
 * @author GOPI
 * 
 */
public abstract class CCDConnector {


	private String ccdId;
	private CloudCommunicationDevice ccd;
	private DeviceClasses deviceClass;
	private CommunicationTypes communicationType;


	public enum DeviceClasses {
		ARDUINO, BEAGLEBONE, ARDUINOYUN, RASPBERRYPI
	};

	public enum CommunicationTypes {
		WIFI, UART, BLUETOOTH, XBEE, ZIGBEE
	};

	public CCDConnector() {

	}

	public abstract void generateCode();

	public abstract void generateBinaryCode();

	public abstract void flashCode();

	public void validate() throws InvalidCommunicationType, InvalidDeviceClass, InValidIdentifier, InvalidConnectionMode
	{
		checkCommunicationType(this.communicationType);
		checkDeviceClass(this.deviceClass);
		if(this.ccdId == null) 
			throw new InValidIdentifier("null");
	}
	public void setCommunicationType(CommunicationTypes communicationType)
			throws InvalidCommunicationType {

		checkCommunicationType(communicationType);
		this.communicationType = communicationType;
	}

	public DeviceClasses getDeviceClass() {
		return deviceClass;
	}

	public void setDeviceClass(DeviceClasses deviceClass)
			throws InvalidDeviceClass {

		checkDeviceClass(deviceClass);

		this.deviceClass = deviceClass;
	}


	public String getCcdId() {
		return ccdId;
	}

	public void setCcdId(String ccdId) {
		this.ccdId = ccdId;
	}

	public CloudCommunicationDevice getCcd() {
		return this.ccd;
	}

	public void setCcd(CloudCommunicationDevice ccd) {
		this.ccdId  = ccd.getCcdId();
		this.ccd = ccd;
	}

	public CommunicationTypes getCommunicationType() {
		return communicationType;
	}

	private void checkDeviceClass(DeviceClasses deviceClass)
			throws InvalidDeviceClass {
		if (deviceClass != DeviceClasses.ARDUINO
				&& deviceClass != DeviceClasses.BEAGLEBONE
				&& deviceClass != DeviceClasses.RASPBERRYPI
				&& deviceClass != DeviceClasses.ARDUINOYUN)
			throw new InvalidDeviceClass(deviceClass != null ? deviceClass.toString():"");
	}

	private void checkCommunicationType(CommunicationTypes communicationType)
			throws InvalidCommunicationType {
		if (communicationType != CommunicationTypes.BLUETOOTH
				&& communicationType != CommunicationTypes.WIFI
				&& communicationType != CommunicationTypes.UART
				&& communicationType != CommunicationTypes.XBEE
				&& communicationType != CommunicationTypes.ZIGBEE)
			throw new InvalidCommunicationType(communicationType != null ? communicationType.toString():"");
	}

	
	public String toString() {
		return "CCDConnector [ccdId=" + ccdId
				+ ", ccd=" + ccd
				+ ", deviceClass=" + deviceClass + ", communicationType="
				+ communicationType + "]";
	}
}
